#
#  pykeeper.py
#  skywarp
#
#  Created by Aaron Cordova on 2/4/09.
#  Copyright (c) 2009 Aaron Cordova. All rights reserved.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
# 
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
# 
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.


__all__ = ['PyKeeperClient', 'setLoggingLevel', 'LOG_LEVEL_ERROR', 'LOG_LEVEL_WARN', 'LOG_LEVEL_INFO', 'LOG_LEVEL_DEBUG', 'CREATED_EVENT', 'DELETED_EVENT', 'CHANGED_EVENT', 'CHILD_EVENT', 'SESSION_EVENT', 'NOTWATCHING_EVENT', 'CREATE_EPHEMERAL', 'CREATE_SEQUENCE']

import _pykeeper

# log levels
LOG_LEVEL_ERROR=1
LOG_LEVEL_WARN=2
LOG_LEVEL_INFO=3
LOG_LEVEL_DEBUG=4

# watch events
CREATED_EVENT=1
DELETED_EVENT=2
CHANGED_EVENT=3
CHILD_EVENT=4
SESSION_EVENT=5    
NOTWATCHING_EVENT=6

# create flags
CREATE_EPHEMERAL=1
CREATE_SEQUENCE=2

def setLoggingLevel(level):
    _pykeeper.setLogLevel(level)

class PyKeeperClient(object):
    def __init__(self, host, watcher):
        '''Connects to a zookeeper host and registers 
        the watcher function as a callback.
        
        The watcher function should take the arguments: type, state, path'''
        self.handle, self.id = _pykeeper.init(host, watcher)    
    
    def close(self):
        _pykeeper.close(self.handle)
        
    def getChildren(self, path, watch=False):
        watchCode = 0
        if watch:
            watchCode = 1
        return _pykeeper.getChildren(self.handle, path, watchCode)
        
    def create(self, path, value='', flags=0):
        return _pykeeper.create(self.handle, path, value, len(value), flags)
    
    # create node and any missing parent nodes
    def createRecursive(self, path, value='', sequence=0, flags=0):
        pass
    
    def delete(self, path, version=-1):
        _pykeeper.delete(self.handle, path, version)

    def exists(self, path, watch=False):
        watchCode = 0
        if watch:
            watchCode = 1
        return _pykeeper.exists(self.handle, path, watchCode)
        
    def get(self, path, watch=False):
        watchCode = 0
        if watch:
            watchCode = 1
        return _pykeeper.get(self.handle, path, watchCode)
        
    def reconnect(self):
        # connect using the stored cid
        pass
        
